﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    public class UmlInterface : DiagramElement
    {
        public UmlInterface(string interfaceName)
            : base(interfaceName)
        {
            MethodList = new ObservableCollection<UmlMethod>();
            MethodList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MethodList_CollectionChanged);
            Metadata.Add("AllowUmlTreeviewDnd", true);
            this.Stereotype = "Interface";
        }


        public ObservableCollection<UmlMethod> MethodList
        {
            get;
            set;
        }

        public List<Object> MethodListView
        {
            get
            {
                if ((this.Metadata.Get<bool>("DeferredLoading")) && (!this.Metadata.Get<bool>("DeferredLoadingComplete")))
                {
//                    DeferredLoadProperties();
                }
                return PropertyGroupingHelper.Group(MethodList.Cast<UmlMemberElement>());
            }
        }

        void MethodList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (UmlMethod p in (sender as IEnumerable<UmlMethod>))
            {
                p.PropertyChanged -= new PropertyChangedEventHandler(MethodListChanged);
                p.PropertyChanged += new PropertyChangedEventHandler(MethodListChanged);
            }
            OnPropertyChanged("MethodListView");
            OnPropertyChanged("Childs");
        }

        void MethodListChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged("MethodListView");
            OnPropertyChanged("Childs");
        }

        protected override ControlTemplate GetTemplate()
        {
            return DiagramElement.DiagramElementTemplateFactory(typeof(UmlInterfaceControl));
        }

        public override ObservableCollection<UmlElement> GetChilds()
        {
            ObservableCollection<UmlElement> elements = base.GetChilds();
            foreach (UmlMethod property in MethodList)
                elements.Add(property);
            return elements;
        }
    }
}
